package com.xinweisi.dataserver.module.memberdata.memberbehavior;

import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.dto.MemberBehaviorQueryDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.entity.MemberBehaviorVO;
import com.xinweisi.dataserver.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/memberBehavior")
public class MemberBehaviorController {
    @Autowired
    MemberBehaviorService memberBehaviorService;
    @Value("${startDBName}")
    private String OldDBName;

    @ApiOperation(value = "分页查会员行为", notes = "分页查会员行为")
    @RequestMapping(value = "/getMemberBehavior",method= RequestMethod.POST)
    public ResponseDTO<PageResultDTO<MemberBehaviorVO>> getMemberBehavior(@RequestBody @Valid MemberBehaviorQueryDTO queryDTO) throws ParseException {
        log.info("----------进入查询会员行为----------");
        String DBNameNew = DateUtil.getDate(new Date());
        String date1=OldDBName.substring(OldDBName.indexOf("_")+1);

        String date2=DBNameNew;

        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");

        Date d1=sdf.parse(date1);

        Date d2=sdf.parse(date2);

        List<String> DBNameList = new ArrayList<String>();

        long daysBetween=(d2.getTime()-d1.getTime()+1000000)/(60*60*24*1000);
        DBNameList.add(OldDBName);
        if(daysBetween>0){
            for (int i = 0;i<daysBetween;i++){
                OldDBName = OldDBName.substring(0,OldDBName.indexOf("_")+1)+(Long.parseLong(OldDBName.substring(OldDBName.indexOf("_")+1))+1);
                DBNameList.add(OldDBName);
            }
        }
        queryDTO.setDBNameList(DBNameList);

        return memberBehaviorService.getMemberBehavior(queryDTO);
    }
}
